package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IPanelService;
import com.mysql.cj.log.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author why
 * @since 2022/07/29 23:02
 */
@Service
public class PanelServiceImpl implements IPanelService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public List<Panel> selectPanel() {

        Example example = new Example(Panel.class);
        example.createCriteria().andNotEqualTo("name","热门推荐");
        List<Panel> panelList = panelMapper.selectByExample(example);

        return panelList;
    }
    @Override
    public List<PanelContentItem> selectPanelContentItems(Integer panelId) {


        List<PanelContentItem> panelContentItems =
                panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
        return panelContentItems;
    }

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        return null;
    }

    @Override
    public NavListResponse selectNavigation() {
        NavListResponse response = new NavListResponse();
        try {
            Example example = new Example(PanelContent.class);
            example.createCriteria().andEqualTo("panelId", 0);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

            ArrayList<PanelContentDto> panelContentDtos = new ArrayList<>();
            for (PanelContent panelContent : panelContents) {
                PanelContentDto panelContentDto = new PanelContentDto();
                BeanUtils.copyProperties(panelContent, panelContentDto);
                panelContentDtos.add(panelContentDto);
            }
            response.setPannelContentDtos(panelContentDtos);
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
